package com.easygem.front.service.imp;

import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.front.LabelInfo;
import com.easygem.common.vo.front.LabelInfoVo;
import com.easygem.front.converter.LabelInfoConverter;
import com.easygem.front.mapper.LabelInfoMapper;
import com.easygem.front.service.LabelInfoService;
import com.easygem.front.service.base.AbstractServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author 何维佳
 * @version 1.0
 * @className LabelInfoServiceImpl
 * @Description TODO
 * @date 2024/12/31 14:37
 */
@Service
public class LabelInfoServiceImpl extends AbstractServiceImpl<LabelInfoMapper, LabelInfo,Integer> implements LabelInfoService {

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Override
    public List<LabelInfo> findLabelInfoList(String accountName) {
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfo(accountName);
        return labelInfoList;
    }
    @Override
    public List<LabelInfoVo> selectLabelInfo(String accountName) {
        return LabelInfoConverter.converterToLabelInfoVoList(labelInfoMapper.selectLabelInfo(accountName));
    }

    /**
     * 添加标签
     * @param labelInfoVo 标签对象
     */
    @Override
    public void add(LabelInfoVo labelInfoVo) {
        labelInfoVo.setCreateTime(new Timestamp(System.currentTimeMillis()));
        LabelInfo labelInfo = new LabelInfo();
        BeanUtils.copyProperties(labelInfoVo,labelInfo);
        labelInfoMapper.insert(labelInfo);
    }

    /**
     * 得到所有标签的所有信息
     *
     * @return 标签集合列表
     */
    @Override
    public List<LabelInfoVo> getLabelInfoAll(String accountName) {
        return LabelInfoConverter.converterToLabelInfoVoList(labelInfoMapper.getLabelInfoList(accountName));
    }
    /**
     * 拿到需要编辑标签的信息
     *
     * @param id id
     * @return
     */
    @Override
    public LabelInfoVo edit(Integer id) throws SystemException {
        LabelInfo labelInfo = labelInfoMapper.selectByPrimaryKey(id);
        if(labelInfo==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的标签不存在");
        }
        return LabelInfoConverter.converterToLabelInfoVo(labelInfo);
    }

    /**
     * 更新标签
     *
     * @param id
     * @param industryVo 标签对象
     */
    @Override
    public void update(Integer id, LabelInfoVo industryVo) throws SystemException {
        LabelInfo labelInfo = labelInfoMapper.selectByPrimaryKey(id);
        if(labelInfo==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的标签不存在");
        }
        LabelInfo labelInfo1 = new LabelInfo();
        BeanUtils.copyProperties(industryVo,labelInfo1);
        labelInfo1.setLabelId(id);
        labelInfoMapper.updateByPrimaryKeySelective(labelInfo1);
    }
}
